﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Maticsoft.Web
{
    public partial class UserAccount : PageBaseMember
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Check();
            UserLeft1.FlagId = "1";
            if (!IsPostBack)
            {
                BindProvince();
                GetUser();
            }
        }

        protected void BindProvince()
        {
            BLL.LocationInfo locBLL = new BLL.LocationInfo();
            List<Model.LocationInfo> list = locBLL.GetModelList("locationId<100");
            ddlProvince.DataSource = list;
            ddlProvince.DataTextField = "locationName";
            ddlProvince.DataValueField = "locationId";
            ddlProvince.DataBind();

            ddlProvince.Items.Insert(0, new ListItem("选择省", "-1"));
        }

        protected void BindCity(int provinceId)
        {
            BLL.LocationInfo locBLL = new BLL.LocationInfo();
            List<Model.LocationInfo> list = locBLL.GetModelList("pId=" + provinceId + " and locationId=cId");
            ddlCity.DataSource = list;
            ddlCity.DataTextField = "locationName";
            ddlCity.DataValueField = "locationId";
            ddlCity.DataBind();

            ddlCity.Items.Insert(0, new ListItem("选择市", "-1"));
        }

        protected void GetUser()
        {
            BLL.Members memBLL = new BLL.Members();
            Model.Members memModel = memBLL.GetModel(int.Parse(UserId));
            ltlUserName.Text = memModel.UserName;
            ltlCreateTime.Text = ((DateTime)memModel.CreateTime).ToString("yyyy-MM-dd");

            txtNickName.Text = memModel.NickName;
            txtRealName.Text = memModel.RealName;
            BLL.LocationInfo locBLL = new BLL.LocationInfo();
            int locId = 0;
            int.TryParse(Convert.ToString(memModel.LocationId), out locId);
            if (locId > 0)
            {
                Model.LocationInfo locModel = locBLL.GetModel(locId);
                ddlProvince.SelectedValue = locModel.pid.ToString();
                BindCity(int.Parse(locModel.pid.ToString()));
                ddlCity.SelectedValue = locId.ToString();
            }
            txtBirthday.Text = memModel.Birthday;
            ddlSex.SelectedValue = memModel.Sex;
            txtPostCode.Text = memModel.PostCode;
            txtAddress.Text = memModel.Address;
            txtQQ.Text = memModel.QQ;
            txtWeixin.Text = memModel.Weixin;
            txtEmail.Text = memModel.Email;
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            BLL.Members memBLL = new BLL.Members();
            Model.Members memModel = memBLL.GetModel(int.Parse(UserId));
            memModel.NickName = txtNickName.Text;
            memModel.RealName = txtRealName.Text;
            memModel.Birthday = txtBirthday.Text;
            memModel.Sex = ddlSex.SelectedValue;
            memModel.Email = txtEmail.Text;
            memModel.LocationId = int.Parse(ddlCity.SelectedValue);
            memModel.Address = txtAddress.Text;
            memModel.PostCode = txtPostCode.Text;
            memModel.QQ = txtQQ.Text;
            memModel.Weixin = txtWeixin.Text;
            memModel.UpdateTime = DateTime.Now;
            memBLL.Update(memModel);
            Common.MessageBox.Show(this.Page, "修改成功");
        }

        protected void ddlProvince_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindCity(int.Parse(ddlProvince.SelectedValue));
        }
    }
}